# -*- coding: utf-8 -*-
"""
Created on Fri Dec  4 15:05:26 2020
find elements
# 显示指定的tag=li,将li中的link地址一一打开
@author: andrew
"""

import time

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome('./diver/chromedriver')
browser = webdriver.Chrome('./diver/chromedriver')
driver.get("http://www.python.org")
driver.set_window_size(1190, 690)
time.sleep(3)
print(driver.title)
# find_elements_by_name
# find_elements_by_xpath
# find_elements_by_link_text
# find_elements_by_partial_link_text
# find_elements_by_tag_name
# find_elements_by_class_name
# find_elements_by_css_selector
# ID = "id"
# XPATH = "xpath"
# LINK_TEXT = "link text"
# PARTIAL_LINK_TEXT = "partial link text"
# NAME = "name"
# TAG_NAME = "tag name"
# CLASS_NAME = "class name"
# CSS_SELECTOR = "css selector"
# <ul class="subnav menu" role="menu" aria-hidden="true">
#         <li class="tier-2 element-1" role="treeitem"><a href="/doc/" title="">Docs</a></li>
#         <li class="tier-2 element-2" role="treeitem"><a href="/doc/av" title="">Audio/Visual Talks</a></li>
#         <li class="tier-2 element-3" role="treeitem"><a href="https://wiki.python.org/moin/BeginnersGuide" title="">Beginner's Guide</a></li>
#         <li class="tier-2 element-4" role="treeitem"><a href="https://devguide.python.org/" title="">Developer's Guide</a></li>
#         <li class="tier-2 element-5" role="treeitem"><a href="https://docs.python.org/faq/" title="">FAQ</a></li>
#         <li class="tier-2 element-6" role="treeitem"><a href="http://wiki.python.org/moin/Languages" title="">Non-English Docs</a></li>
#         <li class="tier-2 element-7" role="treeitem"><a href="http://python.org/dev/peps/" title="">PEP Index</a></li>
#         <li class="tier-2 element-8" role="treeitem"><a href="https://wiki.python.org/moin/PythonBooks" title="">Python Books</a></li>
#         <li class="tier-2 element-9" role="treeitem"><a href="/doc/essays/" title="">Python Essays</a></li>
#         <li class="tier-2 super-navigation">
#                                 <h4>Python’s standard documentation: download, browse or watch a tutorial.</h4>
#                                 <p>Get started below, or visit the <a href="/doc/versions/">Documentation page to browse by version</a>. </p>
#                                 <br>
#                                 <p class="download-buttons">
#                                     <a class="button" href="http://docs.python.org/3/">Python Docs</a> 
#                                 </p>
#                             </li>
# </ul>

try:
    # 显示所有的tag=li
    # elements = driver.find_elements_by_tag_name("li")
    # for element in elements:
    #     print("Value is: ",element.get_attribute("innerHTML"))

    # 显示指定的tag=li,将li中的link地址一个接一个打开
    element = driver.find_element_by_xpath('/html/body/div[1]/header/div/nav/ul')
    # all_lis = element.find_elements_by_tag_name("li")
    all_lis = element.find_elements_by_css_selector("li[class^='tier-1']")

    for li in all_lis:
        # print("Value is: ",li.get_attribute("innerHTML"))
        anchor = li.find_element_by_tag_name('a')
        print("Herf is: ", anchor.get_attribute("href"))
        browser.get(anchor.get_attribute("href"))
        # time.sleep(2)

    # element = driver.find_element(By.XPATH, '/html/body/div/header/div/nav/ul/li[3]/ul')
    # # element = driver.find_element(By.XPATH, '/html/body/div[1]/header/div/nav/ul/li[3]/ul')
    #
    # all_lis = element.find_elements(By.TAG_NAME,"li")
    # for li in all_lis:
    #     print("Value is: ", li.get_attribute("innerHTML"))

    # time.sleep(3)
except Exception as e:
    print(e)
finally:
    browser.quit()
    driver.quit()

